<?php
    //include "../database.php";
    //db_connect();

    //For form to send data
    if(empty($page)) {
	$page = "insertNewSubdivision.php";
    }

    if(!empty($_POST)) {
	$new_subdivision_code = mysql_real_escape_string($_POST['new_subdivision_code']);
	$new_subdivision_title = mysql_real_escape_string($_POST['new_subdivision_title']);
	$query = "SELECT code, title FROM subdivisions WHERE code ='$new_subdivision_code' OR title ='$new_subdivision_title'";
	$result = mysql_query($query);

	//If were found no similar records
	if($new_subdivision_code == "" || $new_subdivision_title == "") {
	     $error_string = "Paliktas tuščias laukelis.";
	} elseif(mysql_num_rows($result) == 0) {
	    $query = "INSERT INTO subdivisions (code, title) VALUES ('$new_subdivision_code', '$new_subdivision_title')";
	    mysql_query($query);
	    $msg = "Įrasas idetas.";
	} else {
	    $row = mysql_fetch_array($result);
	    $error_string = "Įrašas jau egzistuoja:{$row['code']}:{$row['title']}.";
	}
    }

    //db_close();
?>

<label for="error" style="color:red;"><?php if(!empty($error_string)) echo $error_string; ?></label>
<label for="inform" style="color:green;"><?php if(!empty($msg)) echo $msg; ?></label>

<form name="new_subdivision" action="<?php echo $page;?>" method="POST">
    <label>Naujo padalinio kodas:</label><input type="text" name="new_subdivision_code"/></br>
    <label>Naujo padalinio pavadinimas:</label><input type="text" name="new_subdivision_title"/></br>
    <input type="submit" name="submit" value="Įterpti"/>
</form>
<a href="index.php?changes">Atgal</a>